package  main.java.leetcode.editor.cn;
//2022-05-13 19:15:59
//字符串有三种编辑操作:插入一个字符、删除一个字符或者替换一个字符。 给定两个字符串，编写一个函数判定它们是否只需要一次(或者零次)编辑。 
//
// 
//
// 示例 1: 
//
// 输入: 
//first = "pale"
//second = "ple"
//输出: True 
//
// 
//
// 示例 2: 
//
// 输入: 
//first = "pales"
//second = "pal"
//输出: False
// 
// Related Topics 双指针 字符串 👍 189 👎 0

class OneAwayLcci {
    public static void main(String[] args) {
        //创建该题目的对象方便调用
        Solution solution = new OneAwayLcci().new Solution();
        solution.oneEditAway("ab","bc");
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public boolean oneEditAway(String first, String second) {

        int n = first.length();
        int m = second.length();

        if(Math.abs(m-n)>1){
            return false;
        }
        if (n>m){
            return oneEditAway(second,first);
        }

        int i = 0, j = 0, cnt = 0;
        while (i < n && j < m && cnt <= 1) {
            char c1 = first.charAt(i), c2 = second.charAt(j);
            if (c1 == c2) {
                i++; j++;
            } else {
                if (n == m) {
                    i++; j++; cnt++;
                } else {
                    j++; cnt++;
                }
            }
        }
        return cnt <= 1;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
